package org.hxyjs.dao.impl;

import org.hxyjs.dao.UserLikesExpertDao;
import org.hxyjs.entity.UserLikesExpert;
import org.hxyjs.mapper.UserLikesExpertMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("userLikesExpertDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class UserLikesExpertDaoImpl implements UserLikesExpertDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private UserLikesExpertMapper userLikesExpertMapper;

    //查询全部
    public List<UserLikesExpert> getAllUserLikesExperts() throws Exception {
        return userLikesExpertMapper.selectByExample(null);
    }

    //按主键查询
    @Override
    public UserLikesExpert getUserLikesExpertByUlikeexpertno(String ulikeexpertno) throws Exception {
        return userLikesExpertMapper.selectByPrimaryKey(ulikeexpertno);
    }

    //分页查询
    @Override
    public List<UserLikesExpert> getUserLikesExpertByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) userLikesExpertMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return userLikesExpertMapper.getUserLikesExpertByPage(map);
    }


    //删除
    @Override
    public Integer deleteUserLikesExpertByUlikeexpertno(String ulikeexpertno) {
        return userLikesExpertMapper.deleteByPrimaryKey(ulikeexpertno);
    }

    //修改
    @Override
    public Integer updateUserLikesExpertByUlikeexpertno(UserLikesExpert userLikesExpert) {
        return userLikesExpertMapper.updateByPrimaryKey(userLikesExpert);
}


    //增加
    @Override
    public Integer addUserLikesExpert(UserLikesExpert userLikesExpert) {
        userLikesExpert.setUlikeexpertno(initId());
        return userLikesExpertMapper.insertSelective(userLikesExpert);
    }

    public String initId() {
        String maxUlikeexpertno = userLikesExpertMapper.getMaxUlikeexpertno();
        maxUlikeexpertno = Optional.ofNullable(maxUlikeexpertno).orElse("0000");
        return GenNumTools.initId("e", 4, maxUlikeexpertno);
    }


}